package com.zb777.shuzhilingxi.utils;


import com.zb777.shuzhilingxi.Pojo.entity.ChatHistoryDetail;
import com.zb777.shuzhilingxi.service.IChatHistoryDetailService;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Component
@Slf4j
public class MysqlChatMemoryStore implements ChatMemoryStore {

    @Autowired
    private IChatHistoryDetailService chatHistoryDetailService;

    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        Integer chatHistoryListId = (Integer) memoryId;
        List<ChatHistoryDetail> details = chatHistoryDetailService.getByChatHistoryListId(chatHistoryListId);

        List<ChatMessage> messages = new ArrayList<>();
        for (ChatHistoryDetail detail : details) {
            ChatMessage message;
            if ("user".equalsIgnoreCase(detail.getRole())) {
                message = UserMessage.from(detail.getWords());
            } else {
                message = AiMessage.from(detail.getWords());
            }
            messages.add(message);
        }
        return messages;
    }

    @Override
    @Transactional
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        Integer chatHistoryListId = (Integer) memoryId;

        // 先删除原有记录
        chatHistoryDetailService.deleteByChatHistoryListId(chatHistoryListId);

        // 再插入新记录
        for (ChatMessage message : messages) {
            ChatHistoryDetail detail = new ChatHistoryDetail();
            detail.setChatHistoryListId(chatHistoryListId);
            detail.setRole(message instanceof UserMessage ? "user" : "system");
            log.info("信息详情：{}", message.toString());
            String content;
            if (message instanceof UserMessage) {
                content = ((UserMessage) message).singleText();
            } else if (message instanceof AiMessage) {
                content = ((AiMessage) message).text();
            } else {
                content = message.toString();
            }
            detail.setWords(content);
            detail.setCreatedAt(LocalDateTime.now());
            detail.setIsDeleted(false);
            /*if (message instanceof UserMessage && ((UserMessage) message).getContents() instanceof String) {
                detail.setWords((String) ((UserMessage) message).content());
            } else if (message instanceof AiMessage && ((AiMessage) message).content() instanceof String) {
                detail.setWords((String) ((AiMessage) message).content());
            }*/
            log.info("聊天详情：{}", detail);
            chatHistoryDetailService.save(detail);
        }
    }

    @Override
    public void deleteMessages(Object memoryId) {
        Integer chatHistoryListId = (Integer) memoryId;
        chatHistoryDetailService.deleteByChatHistoryListId(chatHistoryListId);
    }
}